﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace sqxt
{
    public class Splasher
    {

        private static Form m_SplashForm = null;
        private static ISplashForm m_SplashInterface = null;
        private static Thread m_SplashThread = null;
        private static string m_TempStatus = string.Empty;
        private static string s1, s2;                      //s1密文，s2密钥，s4身份


        /**/
        /// <summary>
        /// 显示启动画面
        /// </summary>
        public static void Show(Type splashFormType)
        {
            if (m_SplashThread != null)
                return;
            if (splashFormType == null)
            {
                throw (new Exception("必须设置启动窗体"));
            }



            m_SplashThread = new Thread(
                new ThreadStart(delegate()
                {
                    CreateInstance(splashFormType);
                    Application.Run(m_SplashForm);
                }
            ));
            m_SplashThread.IsBackground = true;
            m_SplashThread.SetApartmentState(ApartmentState.STA);
            m_SplashThread.Start();
        }



        /**/
        /// <summary>
        /// 设置加载信息
        /// </summary>
        public static string Status
        {
            set
            {
                if (m_SplashInterface == null || m_SplashForm == null)
                {
                    m_TempStatus = value;
                    return;
                }
                m_SplashForm.Invoke(
                        new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),
                        new object[] { value }
                    );
            }

        }

        /**/
        /// <summary>
        /// 关闭启动画面
        /// </summary>
        public static void Close()
        {
            if (m_SplashThread == null || m_SplashForm == null) return;

            try
            {
                m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));
            }
            catch (Exception)
            {
            }
            m_SplashThread = null;
            m_SplashForm = null;
        }



        private static void CreateInstance(Type FormType)
        {

            object obj = FormType.InvokeMember(null,
                                BindingFlags.DeclaredOnly |
                                BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
            m_SplashForm = obj as Form;
            m_SplashInterface = obj as ISplashForm;
            if (m_SplashForm == null)
            {
                throw (new Exception("启动窗体类型必须是System.Windows.Forms.Form的子类"));
            }
            if (m_SplashInterface == null)
            {
                throw (new Exception("启动窗体必须实现ISplashForm接口"));
            }

            if (!string.IsNullOrEmpty(m_TempStatus))
                m_SplashInterface.SetStatusInfo(m_TempStatus);
        }



        #region  解密算法

        static public string jm(string s1, string s2)
        {

            try
            {
                byte[] DataToDecrypt = Convert.FromBase64String(s1);
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                byte[] bytes_Public_Key = Convert.FromBase64String(s2);
                RSA.ImportCspBlob(bytes_Public_Key);
                byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
                return str_Plain_Text;
            }
            catch (CryptographicException e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }

        #endregion





        public static bool mima(string uid, string pwd)
        {
            try
            {
                SqlConnection conn = new SqlConnection(Dbconnect.s1);
                conn.Open();
                SqlCommand sc = new SqlCommand("select usepwd,miyao from employee where empcode=@usename", conn);
                sc.Parameters.AddWithValue("@usename", uid);
                SqlDataReader sdr = sc.ExecuteReader();
                sdr.Read();
                {
                    s1 = sdr.GetString(0);
                    s2 = sdr.GetString(1);
                }
                sdr.Close();
                string s3 = jm(s1, s2);





                if (pwd == s3)
                {
                    return true;
                }
                else
                {

                    //Mainform mf = new Mainform();
                    //mf.Show();
                    return false;
                }
            }
            catch (Exception)
            {
                login lg = new login();
            
                return false;
            }
        }
        private delegate void SplashStatusChangedHandle(string NewStatusInfo);

    }

}
